//
//  SearchViewController.m
//  礼物go
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchViewController.h"
//#import <CoreText/CoreText.h>
#import "SearchPostViewController.h"
#import "SearchPresentViewController.h"
#import "SearchTableViewCell.h"

#define segmentHeight 45


static NSString *const searchCell = @"searchCell";
@interface SearchViewController ()<UISearchBarDelegate,UISearchResultsUpdating,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UISearchController *searchController;
@property(nonatomic,strong)NSMutableArray *searchArray;

@property(nonatomic,strong)SearchPostViewController *searchPostVC;
@property(nonatomic,strong)SearchPresentViewController *searchPresentVC;
@property(nonatomic,copy)NSString *searchText;

@property(nonatomic,strong)UITableView *searchTableView;
@property(nonatomic,strong)UIView *secondView;
@property(nonatomic,strong)UIView *firstView;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.searchArray = [NSMutableArray array];
    
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    
    //设置点击搜索框时候隐藏导航栏
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    
    //设置开始搜索时背景显示与否页面背景阴影效果,一般不要设置.
    self.searchController.dimsBackgroundDuringPresentation = NO;
    
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
   //[searchBar sizeToFit]:设置searchBar位置自适应
    [self.searchController.searchBar sizeToFit];
    
    //将搜索框放在导航栏上
    self.navigationItem.titleView = self.searchController.searchBar;
    self.searchController.searchBar.delegate = self;
    
    //将导航栏的默认按钮隐藏
    [self.navigationItem setHidesBackButton:YES animated:NO];
    //占位符
    self.searchController.searchBar.placeholder = @"快选一份礼物,送给亲爱的Ta吧";
    self.searchController.searchResultsUpdater = self;
    //显示取消按钮
    self.searchController.searchBar.showsCancelButton = YES;
    self.searchController.searchBar.tintColor = [UIColor whiteColor];
    //self.searchController.searchBar.
    //将英文变成中文
    for(id Chinese in [self.searchController.searchBar.subviews[0] subviews]){
        
        if([Chinese isKindOfClass:[UIButton class]]){
            
            UIButton *button = (UIButton *)Chinese;
            
            [button setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
    
    //创建view
    self.secondView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.secondView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.secondView];

    self.firstView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.firstView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.firstView];
   
    //点击搜索
    self.searchTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.searchTableView.backgroundColor = [UIColor whiteColor];
    [self.firstView addSubview:self.searchTableView];
    self.searchTableView.delegate = self;
    self.searchTableView.dataSource = self;
    [self.searchTableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:searchCell];

    
//搜索结果页面
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"礼物",@"攻略"]];
    segment.frame = CGRectMake(0, 0, SCREENWIDTH, segmentHeight);
    segment.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFF0000,0x1C1C1C);
    //segment.backgroundColor = [UIColor redColor];
    segment.tintColor = [UIColor whiteColor];
    [self.secondView addSubview:segment];
    [segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    [segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15],NSForegroundColorAttributeName:[UIColor redColor]} forState: UIControlStateSelected];

    self.searchPostVC = [[SearchPostViewController alloc] init];
    [self addChildViewController:self.searchPostVC];
    [self.secondView addSubview:self.searchPostVC.view];
    
    self.searchPresentVC = [[SearchPresentViewController alloc] init];
    [self addChildViewController:self.searchPresentVC];
    [self.secondView addSubview:self.searchPresentVC.view];
    
    [self.secondView bringSubviewToFront:segment];
    [segment addTarget:self action:@selector(SegmentAction:) forControlEvents:UIControlEventValueChanged];
    //设置segment默认选中状态
    segment.selectedSegmentIndex = 0;
    
    
    
    
    // Do any additional setup after loading the view.
}


-(void)SegmentAction:(UISegmentedControl *)segment{
    [self.secondView bringSubviewToFront:segment];
    //切换页面
    switch (segment.selectedSegmentIndex) {
        case 0:
            [self.secondView bringSubviewToFront:self.searchPresentVC.view];
            break;
        case 1:
            [self.secondView bringSubviewToFront:self.searchPostVC.view];
            break;
            
        default:
            break;
    }
    
    [self.secondView bringSubviewToFront:segment];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
   
    [self.navigationController popViewControllerAnimated:YES];
    //隐藏键盘
    [searchBar resignFirstResponder];
    
}

//实现监听输入完毕的方法
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    //移动最上层
    [self.view bringSubviewToFront:self.secondView];
 //获取输入框内容
    self.searchText = [searchBar text];
    
    self.searchPostVC.searchPostString = self.searchText;
    self.searchPresentVC.searchPresentString = self.searchText;
    
    return YES;
    
}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    [self.searchController.searchBar sizeToFit];
    [self.view bringSubviewToFront:self.firstView];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchCell];

    cell.keywordBlock = ^(NSString *key){
       self.searchController.searchBar.text = key;
        [self searchBarShouldEndEditing:self.searchController.searchBar];
        [self.searchController.searchBar resignFirstResponder];
    };
    
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
